﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

// TODO: Complete documentation
namespace ComicTracker.Models
{
    [Table("issues")]
    public class Issue : IComparable<Issue>
    {
        private Guid _id;
        private Guid _series_id;
        private int _issue_number;
        private bool _variant;
        private DateTime _release_date;
        private string _edition;
        private string _title;
        private string _subtitle;
        private int _num_pages;
        private decimal _cover_price;
        private Series _series;

        #region Properties

        [Key]
        [Column("id")]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        [Column("series_id")]
        [Required(ErrorMessage="A series id is required.")]
        public Guid SeriesID
        {
            get { return _series_id; }
            set { _series_id = value; }
        }

        [Column("issue_number")]
        [Required(ErrorMessage="An issue number is required.")]
        [Display(Name="Issue Number")]
        public int IssueNumber
        {
            get { return _issue_number; }
            set { _issue_number = value; }
        }

        [Column("variant")]
        [Required(ErrorMessage="An issue must be marked as either a regular cover or a variant.")]
        public bool Variant
        {
            get { return _variant; }
            set { _variant = value; }
        }

        [Column("release_date")]
        [Required(ErrorMessage = "A release date is required.")]
        [Display(Name = "Release Date")]
        public DateTime ReleaseDate
        {
            get { return _release_date; }
            set { _release_date = value; }
        }

        [Column("edition")]
        public string Edition
        {
            get { return _edition; }
            set { _edition = value; }
        }

        [Column("title")]        
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [Column("subtitle")]
        public string Subtitle
        {
            get { return _subtitle; }
            set { _subtitle = value; }
        }

        [Column("num_pages")]
        [Display(Name="Number of Pages")]
        public int? NumPages
        {
            get { return _num_pages; }
            set 
            { 
                if (value != null)
                    _num_pages = value.Value; 
            }
        }

        [Column("cover_price")]
        [Display(Name="Cover Price")]
        public decimal? CoverPrice
        {
            get { return _cover_price; }
            set 
            { 
                if (value != null)
                    _cover_price = value.Value; 
            }
        }

        public virtual Series Series
        {
            get { return _series; }
            set { _series = value; }
        }
        #endregion

        public int CompareTo(Issue other)
        {
            if (other == null)
                return 1;

            // Compare series 
            int cmp = this.Series.CompareTo(other.Series);
            if (cmp == 0)
            {
                // Compare issue numbers
                if (this.IssueNumber != other.IssueNumber)
                {
                    if (this.Variant && !other.Variant)
                        return -1;
                    else if (!this.Variant && other.Variant)
                        return 1;
                    else
                        return this.ReleaseDate.CompareTo(other.ReleaseDate);
                }
                return 0;
            }

            return cmp;
        }
    }
}